<?php
class indentController extends Controller {

    function __construct() {
        parent::__construct();
    }

    //订单付款
    function pay() {
        $this->debugmsg('indentController::pay');

        $data['id']     = intval($this->request['id']);
        $data['ok']     = $this->request['ok'];

        $data['sid']    = $this->session['sid'];
        $data['uid']    = $this->session['id'];

        /*
        1.合法
        2.非法
        */

        if (!$data['id'] || !$data['sid'])
            $result['code']['pay'] = 2;
        else
            $result = $this->model->pay($data);

        $result['data'] = $data;

        return $result;
    }

    //在线付款
    function payNow() {
        $this->debugmsg('indentController::payNow');

        /*
        type:
        1.云网
        2.支付宝
        3.快钱
        */

        /*
        1.成功
        2.参数不全
        4.非有效定单
        */
        if ($this->request['id'] && $this->request['type'] <= 10) {
            $data['id']     = intval($this->request['id']);
            $data['type']   = intval($this->request['type']);

            $result = $this->model->payNow($data);
        } else {
            $result['code']['payNow'] = 2;
        }

        return $result;
    }

    //付款结果
    function payResult() {
        $this->debugmsg('indentController::payNow');

        /*
        1.cncard
        2.alipay
        */
        $data['type'] = intval($this->request['type']);

        if (!$data['type'])
            $result['code']['payResult'] = 2;
        else
            $result = $this->model->payResult($data);

        //付款成功处理
        if ($result['code']['payResult'] == 1) {
            do_action('indent_payed', $d);
            unset($d);
        }

        return $result;
    }

    function adminList() {
        $this->debugmsg('indentController::adminList');

        //定单清理
        $this->userIndentClear();

        $data['sort']       = ($this->request['sort'] ? strtolower($this->request['sort']) : '');
        $data['sortorder']  = ($this->request['sortorder'] ? strtoupper($this->request['sortorder']) : 'DESC');
        $data['pagesize']   = ($this->request['pagesize'] ? intval($this->request['pagesize']) : $this->pagesize);

        $data['where']      = 'stat < 3 AND stat <> -1';
        if (($data['stat'] = $this->request['show']) == 'archive') $data['where'] = 'stat=-1';
        if (($data['stat'] = $this->request['show']) == 'all') $data['where'] = 'stat<>-1';

        if ($this->request['s']) $data['search'] = trim($this->request['s']);

        if ($this->request['ids']) {
            $this->request['ids'] = explode(',', $this->request['ids']);
            foreach ($this->request['ids'] as $id) $tmp[] = intval($id);
            $data['ids'] = join(',', $tmp);
        }

        $result['data'] = $data;
        $result['list'] = $this->model->indentsList($data);

        return $result;
    }

    function adminEdit() {
        $this->debugmsg('indentController::adminEdit');

        /*
        1.成功
        2.失败
        */
        //管理员标志
        $data['admin']  = $this->session['admin'];

        //用户ID
        $data['uid']    = $this->session['id'];

        $data['id']     = $this->request['id'];
        $data['remove'] = $this->request['remove'];
        $data['add']    = $this->request['add'];

        $result['data'] = $data;

        if (isset($this->post['stat']) && $this->post['linkman'] && $this->post['address'] || $data['remove']) {

            $data['linkman']    = trim($this->post['linkman']);
            $data['address']    = trim($this->post['address']);
            $data['tel']        = trim($this->post['tel']);
            $data['zipcode']    = trim($this->post['zipcode']);

            $data['adjustfee']  = floatval($this->post['adjustfee']);
            $data['postfee']    = floatval($this->post['postfee']);
            $data['stat']       = intval($this->post['stat']);

            $data['posttype']   = trim($this->post['posttype']);
            $data['postnum']    = trim($this->post['postnum']);

            //延时订单
            if ($this->post['created']==1) $data['created'] = time();
            $data['updated']    = time();

            $result = $this->model->indentsUpdate($data);
            $result['ajaxcode'] = 1;

            if ($data['remove'] || $result['add']) $this->redirect('indents.php');
        } else {
            $result['ajaxcode'] = 2;
        }

        if (!$data['add']) {
            $v['id'] = $data['id'];
            $result['data'] = $this->model->indentsList($v);
        }

        return $result;
    }

    //我的订单列表
    function userIndent() {
        $this->debugmsg('indentController::userIndent');

        $this->needLogin();

        //定单更新
        if ($this->post) $r = $this->userIndentUpdate();

        if ($this->request['id'])
            $result = $this->userIndentView();
        else {
            if ($this->request['show'] == 'archive') {
                $result = $this->userArchiveIndent();
                $result['data']['archive'] = true;
            } else {
                //定单清理
                /*条件：把超过三天未付款定单状态改为存档定单，其它状态定单不变*/
                /*同时库存返还*/
                $this->userIndentClear();

                $result = $this->userActiveIndent();
            }
        }

        if ($this->post) $result['ajaxcode'] = $r['code']['indentsUpdate'];

        return $result;
    }

    //单个订单
    function userIndentView() {
        $this->debugmsg('indentController::userIndentView');

        $data['uid']    = $this->session['id'];
        $data['id']     = intval($this->request['id']);

        $result = $this->model->indentsList($data);
        $result['data'] = $data;

        return $result;
    }

    //活动订单
    function userActiveIndent() {
        $this->debugmsg('indentController::userActiveIndent');

        $data['uid']        = $this->session['id'];
        $data['where']      = "stat <> -1";
        $data['pagesize']   = 10;
        $data['sort']       = 'updated';
        $data['sortorder']  = 'DESC';

        $result = $this->model->indentsList($data);
        $result['data'] = $data;

        return $result;
    }

    //存档订单
    function userArchiveIndent() {
        $this->debugmsg('indentController::userArchiveIndent');

        $data['uid']        = $this->session['id'];
        $data['where']      = "stat = -1";
        $data['pagesize']   = 15;
        $data['sortorder']  = 'DESC';

        $result = $this->model->indentsList($data);
        $result['data'] = $data;

        return $result;
    }

    /*
    TODO ajax 更新订单状态 2005-11-16
    */
    function ajaxuserIndent($var) {
        parse_str($var, $this->post);
        $this->post = array_stripslashes($this->post);

        print_r($this->post);
        exit;

        $result = $this->userIndent();
        $this->ajax->quickresult($result['ajaxcode']);
    }

    function userIndentUpdate() {
        $this->debugmsg('indentController::userIndent');

        $this->needLogin();

        $data['id']     = intval($this->request['id']);
        $data['uid']    = intval($this->session['id']);
        $data['admin']  = $this->session['admin'];

        if (isset($this->request['linkman']))   $data['linkman']    = trim($this->request['linkman']);
        if (isset($this->request['tel']))       $data['tel']        = trim($this->request['tel']);
        if (isset($this->request['address']))   $data['address']    = trim($this->request['address']);
        if (isset($this->request['zipcode']))   $data['zipcode']    = intval($this->request['zipcode']);
        if (isset($this->request['memo']))      $data['memo']       = trim($this->request['memo']);

        /*
        Indent stat:
        -1.定单过期
        0.未支付
        1.已支付
        2.已收款
        3.已发货
        4.定单完成
        */
        if ($this->request['stat0'])    $data['stat'] = 1;
        if ($this->request['stat1'])    $data['stat'] = 0;
        if ($this->request['stat3'])    $data['stat'] = 3;
        if ($this->request['stat4'])    $data['stat'] = 4;
        if ($this->request['stat-1'])   $data['stat'] = -1;

        $result = $this->model->indentsUpdate($data);
        $result['data'] = $data;

        return $result;
    }

    //清理定单
    function userIndentClear() {
        $this->debugmsg('indentController::userIndentClear');

        return $this->model->indentsClear();
    }

}
